using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using HuaRongDaoGame.Models;

namespace HuaRongDaoGame.Forms
{
    /// <summary>
    /// 布局选择窗体
    /// </summary>
    public partial class LayoutSelectionForm : Form
    {
        public GameLayout SelectedLayout { get; private set; }

        public LayoutSelectionForm()
        {
            InitializeForm();
        }

        /// <summary>
        /// 初始化窗体
        /// </summary>
        private void InitializeForm()
        {
            this.Text = "选择游戏布局";
            this.StartPosition = FormStartPosition.CenterParent;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Size = new Size(500, 400);
            this.BackColor = Color.LightGray;

            CreateControls();
        }

        /// <summary>
        /// 创建控件
        /// </summary>
        private void CreateControls()
        {
            // 标题标签
            var titleLabel = new Label
            {
                Text = "请选择游戏布局",
                Font = new Font("微软雅黑", 16, FontStyle.Bold),
                ForeColor = Color.DarkBlue,
                TextAlign = ContentAlignment.MiddleCenter,
                Size = new Size(400, 30),
                Location = new Point(50, 20)
            };

            // 布局列表
            var layoutListBox = new ListBox
            {
                Font = new Font("微软雅黑", 10),
                Size = new Size(400, 250),
                Location = new Point(50, 60),
                SelectionMode = SelectionMode.One
            };

            // 获取所有布局
            var layouts = ClassicLayouts.GetAllLayouts();
            foreach (var layout in layouts)
            {
                layoutListBox.Items.Add(string.Format("{0} - {1} - {2}", layout.Name, layout.GetDifficultyDescription(), layout.Description));
            }

            // 默认选择第一个
            if (layoutListBox.Items.Count > 0)
            {
                layoutListBox.SelectedIndex = 0;
            }

            // 确定按钮
            var okButton = new Button
            {
                Text = "确定",
                Font = new Font("微软雅黑", 10),
                Size = new Size(80, 30),
                Location = new Point(200, 320),
                BackColor = Color.LightBlue,
                FlatStyle = FlatStyle.Flat,
                DialogResult = DialogResult.OK
            };
            okButton.Click += (sender, e) =>
            {
                if (layoutListBox.SelectedIndex >= 0)
                {
                    SelectedLayout = layouts[layoutListBox.SelectedIndex];
                }
            };

            // 创建自定义布局按钮
            var customLayoutButton = new Button
            {
                Text = "创建自定义布局",
                Font = new Font("微软雅黑", 10),
                Size = new Size(120, 30),
                Location = new Point(50, 320),
                BackColor = Color.LightGreen,
                FlatStyle = FlatStyle.Flat
            };
            customLayoutButton.Click += (sender, e) =>
            {
                var customLayoutForm = new CustomLayoutForm();
                if (customLayoutForm.ShowDialog() == DialogResult.OK)
                {
                    // 刷新布局列表
                    layoutListBox.Items.Clear();
                    var allLayouts = ClassicLayouts.GetAllLayouts();
                    var customLayouts = HuaRongDaoGame.Services.GameDataService.LoadCustomLayouts();
                    allLayouts.AddRange(customLayouts);
                    
                    foreach (var layout in allLayouts)
                    {
                        layoutListBox.Items.Add(string.Format("{0} - {1} - {2}", layout.Name, layout.GetDifficultyDescription(), layout.Description));
                    }
                    
                    if (layoutListBox.Items.Count > 0)
                    {
                        layoutListBox.SelectedIndex = 0;
                    }
                }
            };

            // 取消按钮
            var cancelButton = new Button
            {
                Text = "取消",
                Font = new Font("微软雅黑", 10),
                Size = new Size(80, 30),
                Location = new Point(300, 320),
                BackColor = Color.LightCoral,
                FlatStyle = FlatStyle.Flat,
                DialogResult = DialogResult.Cancel
            };

            // 添加控件
            this.Controls.AddRange(new Control[]
            {
                titleLabel,
                layoutListBox,
                okButton,
                cancelButton,
                customLayoutButton
            });

            // 设置默认按钮
            this.AcceptButton = okButton;
            this.CancelButton = cancelButton;
        }
    }
} 